package com.zzk.www.tool.common;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzk on 2017/12/26.
 */
public class JsonUtils {

    private static Logger log = Logger.getLogger(JsonUtils.class);

    public static <T> T json2Object(String json, Class<T> clazz) {
        if(StringUtils.isBlank(json)) {
            return null;
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            Object obj = null;
            try {
                obj = objectMapper.readValue(json, clazz);
            } catch (Exception var5) {
                log.error(var5.getMessage(), var5);
            }

            return (T)obj;
        }
    }

    public static <T> List<T> json2List(String json, Class<T> clazz) {
        if(StringUtils.isBlank(json)) {
            return null;
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            List obj = null;

            try {
                obj = (List)objectMapper.readValue(json, (new ObjectMapper()).getTypeFactory().constructParametricType(ArrayList.class, new Class[]{clazz}));
            } catch (Exception var5) {
                log.error(var5.getMessage(), var5);
            }

            return obj;
        }
    }

    public static String object2Json(Object object) {
        if(object == null) {
            return "";
        } else {
            ObjectMapper objectMapper = new ObjectMapper();

            try {
                return objectMapper.writeValueAsString(object);
            } catch (Exception var3) {
                log.error(var3.getMessage(), var3);
                return "";
            }
        }
    }




}
